 

import Control.*;
import Model.*;

/**
 * UI for Orders
 * @author Stefan
 */
public class OrderUI {

    private TradeController tradeCtl = TradeController.getInstance();
    private AssociateController assCtr = AssociateController.getInstance();
    private ItemsUI itemsUI = new ItemsUI();

    /**
     * Start Order menu
     */
    public void start()
    {
        orderMenu();
    }

    /**
     * Order menu
     */
    private void orderMenu()
    {
        boolean exit = false;
        while (!exit)
        {
            int choise = writeOrderMenu();
            if (choise == 1)
            {
                startCreate(CommonUIFunctions.readString("\n write order ID: "), CommonUIFunctions.readString("\n write delivery date: "), CommonUIFunctions.readString("\n write delivery address: "), CommonUIFunctions.readString("\n write employee ID: "), CommonUIFunctions.readString("\n customer ID: "));
            }
            else if (choise == 2)
            {
                startRead(CommonUIFunctions.readString("\n write ID: "));

            }
            else if (choise == 3)
            {
                startUpdate(CommonUIFunctions.readString("\n write ID: "));

            }
            else if (choise == 4)
            {
                startAddItem(CommonUIFunctions.readString("\n write ID: "));
            }
            else if (choise == 5)
            {
                startDelete(CommonUIFunctions.readString("\n write ID: "));
            }
            else
            {
                exit = true;
            }
        }
    }

    /**
     * Print Order menu
     * @return int based on your choise
     */
    private int writeOrderMenu()
    {
        System.out.println("\f *** Order Menu ***");
        System.out.println(" (1) Create");
        System.out.println(" (2) Read");
        System.out.println(" (3) Update");
        System.out.println(" (4) Add items to order");
        System.out.println(" (5) Delete");
        System.out.println(" (0) Back");

        return CommonUIFunctions.readInt("\n Make your choise: ");
    }

    /**
     * Get info about Order
     * @param ID
     * @return Order
     */
    private Order getInfo(String ID)
    {
        Order order = tradeCtl.findOrder(ID);
        System.out.println("**Order**");
        System.out.println("ID: " + order.getId());
        System.out.println("Customer: " + assCtr.findCustomerID(order.getCustomerID()).getName());
        System.out.println("Delivery date: " + order.getDeliveryDate());
        System.out.println("Delivery address: " + order.getDeliveryAddress());
        for (SalesLineItem item : order.getSaleLines())
        {
            System.out.println(item.getProduct().getName() + ": " + item.getQuantity() + " / " + item.getProduct().getPrice() + " = " + item.subTotalPrice());
        }
        System.out.println("Total price: " + order.totalPrice());
        System.out.println("Employee: " + assCtr.findEmployeeID(order.getEmployeeID()).getName());
        return order;

    }

    /**
     * Create menu
     * @param id
     * @param deliveryDate
     * @param deliveryAddress
     * @param empId
     * @param cusId
     */
    private void startCreate(String id, String deliveryDate, String deliveryAddress, String empId, String cusId)
    {
        int status = tradeCtl.addOrder(id, deliveryDate, deliveryAddress, empId, cusId);
        if (status == 0)
        {
            //Run same method again if employee ID dosn't exists
            startCreate(id, deliveryDate, deliveryAddress, CommonUIFunctions.readString("There was no employee with this ID choose another: "), cusId);
        }
        if (status == 1)
        {
            //Run same method again if no customer with given ID
            startCreate(id, deliveryDate, deliveryAddress, empId, CommonUIFunctions.readString("There was no customer with this ID, choose another: "));
        }
        if (status == 2)
        {
            //Run method again if Order ID is already in use
            startCreate(CommonUIFunctions.readString("Order ID is already in use, choose another: "), deliveryDate, deliveryAddress, empId, cusId);
        }
    }

    /**
     * Read menu
     * @param ID
     */
    private void startRead(String ID)
    {
        try
        {
            getInfo(ID);
        }
        catch (Exception e)
        {
            System.out.println("There was no order with this ID: " + (ID));
        }
    }

    /**
     * Delete menu
     * @param ID
     */
    private void startDelete(String ID)
    {
        try
        {
            getInfo(ID);
            tradeCtl.deleteTrade(ID);
            System.out.println("Trade " + (ID) + " was deleted");
        }
        catch (Exception e)
        {
            System.out.println("There was no trade with this ID: " + (ID));
        }

    }

    /**
     * Update menu
     * @param ID
     */
    private void startUpdate(String ID)
    {
        try
        {
            updateMenu(ID);
        }
        catch (Exception e)
        {
            System.out.println("There was no trade with this ID: " + (ID));
        }
    }

    /**
     * Add item menu
     * @param ID
     */
    private void startAddItem(String ID)
    {
        if (tradeCtl.findOrder(ID) != null)
        {
            // If false write error message
            if (tradeCtl.addSaleItem((CommonUIFunctions.readString("\n write ID of product: ")), CommonUIFunctions.readInt("\n write quantity: "), ID) == false)
            {
                System.out.println("check out the products in stock");
            }
        }
        else
        {
            //Run same method again if there was no Order with given ID
            startAddItem(CommonUIFunctions.readString("There was no order with this ID:" + ID + "\n try another ID: "));
        }
    }

    /**
     * Update menu
     * @param ID
     */
    private void updateMenu(String ID)
    {
        boolean exit = false;
        while (!exit)
        {
            Order ord = tradeCtl.findOrder(ID);
            int choise = writeUpdateMenu();
            if (choise == 1)
            {
                ord.setId(CommonUIFunctions.readString("\n write new ID: "));
            }
            else if (choise == 2)
            {
                ord.setDeliveryDate(CommonUIFunctions.readString("\n write new delivery date: "));
            }
            else if (choise == 3)
            {
                ord.setDeliveryAddress(CommonUIFunctions.readString("\n write new delivery address: "));
            }
            else if (choise == 4)
            {
                itemsUI.start(ID);
            }
            else
            {
                exit = true;
            }
        }
    }

    /**
     * Print update menu
     * @return int based on your choise
     */
    private int writeUpdateMenu()
    {
        System.out.println("\n\f *** UpdateMenu ***");
        System.out.println("\f Choose one to edit");
        System.out.println(" (1) Id ");
        System.out.println(" (2) Delivery date");
        System.out.println(" (3) Delivery address");
        System.out.println(" (4) Edit Items");
        System.out.println(" (0) Back");

        return CommonUIFunctions.readInt("\n Make your choise: ");
    }
}
